<?php

	class BDD {
		private static $host = 'localhost';
		private static $dbname = 'cafe';
		private static $user = 'root';
		private static $pass = '';
		
		private static function getPointer() {
			$pointer = new PDO('mysql:host=' . self::$host . ';dbname=' . self::$dbname . '', '' . self::$user . '', '' . self::$pass);
			$pointer->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			return $pointer;
		}
		
		public static function query($query,$querytype = 0) {
			$tab = array();
			$error;
			$pointer = self::getPointer();
			$type = explode(" ",$query);
			$type = strtoupper($type[0]);
			try 
			{
				$type=='SELECT' ? $res = $pointer->query($query) : $pointer -> exec($query) ;
				if($type == 'SELECT') 
				{
					$c = 0;
					while($tmp = $res->fetch())
					{
						$tab2 = array();
						foreach($tmp as $K => $V)
						{
							if(!is_int($K))
								$tab2["$K"] = $V;
						}
						$tab[$c++] = $tab2;
					}					
					if($querytype)
					{
						if($querytype==1)
						{
							foreach ($tab[0] as $K => $V)
							{
								$tab = $V;
							}
						}
						if($querytype==2)
						{ 
							$tab = $tab[0];
						}
					}
				}
				else 
				{
					if ($type == 'INSERT')
					{
						$tab["ID"] = $pointer -> lastInsertId();
					}
				}	
				$pointer = null;
				
				return $tab;				
			}			
			catch(Exception $e) {
				$error = $pointer -> errorInfo();
				$pointer = null;
				$error["error"] = $e->getMessage();
				if($error['0'] !='0000')
					return $error;
			}
		}
	}
?>